package se.statifyme.client.declaration;

import java.util.Collection;

import se.statifyme.client.record.RecordClient;
import se.thoka.twt.client.json.TWTJSONParser;
import se.thoka.twt.client.utils.TWTLogLevel;
import se.thoka.twt.client.utils.TWTLogger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

public class TestDeclaredItemForm extends Composite {
	
	interface Binder extends UiBinder<Widget, TestDeclaredItemForm> {}
	
	@UiField
	HasText jsonField;
	
	@UiField
	Button generateButton;
	
	@UiField
	ListBox serviceListBox;
	
	@UiField
	DeclaredItemForm form;
	
	private final int firstListBoxId;
	
	public TestDeclaredItemForm() {
		initWidget(GWT.<Binder>create(Binder.class).createAndBindUi(this));
		
		firstListBoxId = serviceListBox.getItemCount();
		serviceListBox.setEnabled(false);
		String serviceDeclarationsURI = RecordClient.getServiceDeclarationsURI();
		RecordClient.INSTANCE.get(serviceDeclarationsURI, new AsyncCallback<String>() {
			@Override
			public void onSuccess(String result) {
				serviceListBox.setEnabled(true);
				JSONValue jsonValue = JSONParser.parseStrict(result);
				Collection<GWTServiceDeclaration> serviceDeclarations = TWTJSONParser.parseArray(GWTServiceDeclaration.class, jsonValue);
				TWTLogger.log(serviceDeclarations.size() + " service declarations from: " + result);
				for (GWTServiceDeclaration declaration : serviceDeclarations) {
					serviceListBox.addItem(declaration.getDisplayName(), declaration.getPath());
				}
			}
			@Override
			public void onFailure(Throwable caught) {
				TWTLogger.log(TWTLogLevel.ERROR, "Unable to fetch service declarations", caught);
			}
		});
	}
	
	@UiHandler(value = "serviceListBox")
	void onServiceListChanged(ChangeEvent event) {
		int selectedIndex = serviceListBox.getSelectedIndex();
		jsonField.setText("");
		if (selectedIndex >= firstListBoxId) {
			serviceListBox.setEnabled(false);
			final String servicePath = serviceListBox.getValue(selectedIndex);
			final String uri = RecordClient.buildServiceDeclarationURI(servicePath);
			RecordClient.INSTANCE.get(uri, new AsyncCallback<String>() {
				@Override
				public void onSuccess(String result) {
					jsonField.setText(result);
					serviceListBox.setEnabled(true);
				}
				
				@Override
				public void onFailure(Throwable caught) {
					TWTLogger.log(TWTLogLevel.ERROR, "Unable to get service declaration at '" + uri + "'", caught);
					jsonField.setText( "Unable to get service declaration at '" + uri + "'");
					serviceListBox.setEnabled(true);
				}
			});
		}
	}
	
	@UiHandler(value = "generateButton")
	void onGenerateClicked(ClickEvent event) {
		String json = jsonField.getText();
		try {
			JSONValue jsonValue = JSONParser.parseStrict(json);
			form.setJSONItemDeclaration(jsonValue);
		} catch (Exception e) {
			TWTLogger.log("Unable to generate item declaration.", e);
		}
	}
}
